package order.servlet;

import lombok.SneakyThrows;
import order.dao.adminDao;
import order.pojo.customer;
import order.utils.DbUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class adminServlet  extends HttpServlet {


    DbUtil dbUtil = new DbUtil();
    adminDao adminDao = new adminDao();


    @SneakyThrows
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //分发不同的请求
        req.setCharacterEncoding("utf-8");
        HttpSession session = req.getSession();

        String action = req.getParameter("action");
        if("querry".equals(action)) {
            querryAllCustomer(req,resp);
            return;
        } else if("delete".equals(action)){
            deleteCustomerById(req,resp);
            return;
        } else if("add".equals(action)){
            addCustomer(req,resp);
            return;
        } else if("update".equals(action)){
            updateCustomer(req,resp);
            return;
        }
    }



    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //分发不同的请求

        HttpSession session = req.getSession();

        String action = req.getParameter("action");
        if("querry".equals(action)) {
            querryAllCustomer(req,resp);
            return;
        } else if("delete".equals(action)){
            deleteCustomerById(req,resp);
            return;
        } else if("add".equals(action)){
            addCustomer(req,resp);
            return;
        } else if("update".equals(action)){
            updateCustomer(req,resp);
            return;
        }






    }
    //显示所有顾客
    private void querryAllCustomer(HttpServletRequest req,HttpServletResponse resp){
        Connection con = null;

        try {
            System.out.println("a=>进入了querry的servlet");
            con = dbUtil.getCon();
//            list = adminDao.querryAllAccount(con);
            List<customer> list = adminDao.querryAllAccount(con);
            req.setAttribute("list", list);
            System.out.println("a=>list"+list.toString());
            list.get(1);
//resp.sendRedirect("admin/formShow.jsp");
            req.getRequestDispatcher("admin/formShow.jsp").forward(req,resp);


        } catch (Exception e) {

        } finally {
            try {
                dbUtil.closeCon(con);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


    }

    private void deleteCustomerById(HttpServletRequest req,HttpServletResponse resp)  {
        System.out.println("=>进入了删除servlet");
    String customerId=req.getParameter("customerId");
    Connection con=null;
    try{
        con=dbUtil.getCon();
        adminDao.deleteCustomerById(con,customerId);
        req.getRequestDispatcher("admin?action=querry").forward(req,resp);
        System.out.println("=>完成了请求的转发");

    }catch (Exception e){

    }finally {
        try {
            dbUtil.closeCon(con);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    }
    //修改
    private void updateCustomer(HttpServletRequest req,HttpServletResponse resp) throws Exception {
        System.out.println("=>进入了修改servlet");
        customer customer=null;
        String UpcustomerId=req.getParameter("UpcustomerId");
        String customerId=req.getParameter("customerId");
        String customer_account=req.getParameter("customer_account");
        String customer_password=req.getParameter("customer_password");

//        System.out.println("UpcustomerId:"+UpcustomerId);
//        System.out.println("customerId:"+customerId);

       customer=new customer(Integer.valueOf(customerId),customer_account,customer_password);
        System.out.println(customer.toString());

        Connection con=null;
        con=dbUtil.getCon();
        adminDao.updateCustomer(con,customer,UpcustomerId);
        req.getRequestDispatcher("admin?action=querry").forward(req,resp);

    }
    //增加
    private void addCustomer(HttpServletRequest req,HttpServletResponse resp) throws Exception {


        System.out.println("=>进入了添加的servlet");
        customer customer=null;
        String customerId=req.getParameter("customerId");
        String customer_account=req.getParameter("customer_account");
        String customer_password=req.getParameter("customer_password");
        customer=new customer(Integer.valueOf(customerId),customer_account,customer_password);
        System.out.println(customer.toString());
        Connection con=null;
        con=dbUtil.getCon();
        adminDao.addCustomer(con,customer);

        req.getRequestDispatcher("admin?action=querry").forward(req,resp);

    }



}
